package com.thb.pay.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.thb.pay.api.sys.SysUserServiceApi;
import com.thb.pay.api.sys.vo.req.SysUserPageReq;
import com.thb.pay.api.sys.vo.req.SysUserReq;
import com.thb.pay.api.sys.vo.req.SysUserRoleReq;
import com.thb.pay.api.sys.vo.resp.SysUserRoleVo;
import com.thb.pay.api.sys.vo.resp.SysUserVo;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.system.entity.SysUser;
import com.thb.pay.system.entity.SysUserRole;
import com.thb.pay.system.service.SysUserRoleService;
import com.thb.pay.system.service.SysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统-用户表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@RestController
public class SysUserController implements SysUserServiceApi {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Override
    public PageResult<SysUserVo> findPage(@RequestBody SysUserPageReq pageReq) {
        return sysUserService.findPage(pageReq);
    }

    @Override
    public void addUser(@RequestBody SysUserReq sysUser) {
        SysUser targetUser = new SysUser();
        BeanUtils.copyProperties(sysUser, targetUser);
        sysUserService.insert(targetUser);
    }

    @Override
    public void updateUser(@RequestBody SysUserReq sysUser) {
        SysUser targetUser = new SysUser();
        BeanUtils.copyProperties(sysUser, targetUser);
        sysUserService.updateById(targetUser);
    }

    @Override
    public SysUserVo getByUserId(@RequestParam("userId") Long userId) {
        SysUser resResult = sysUserService.getById(userId);
        if (resResult != null) {
            SysUserVo targetResult = new SysUserVo();
            BeanUtils.copyProperties(resResult, targetResult);
            return targetResult;
        }
        return null;
    }

    @Override
    public SysUserVo getByUserName(@RequestParam("userName") String userName) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUserName, userName);
        List<SysUser> resResultList = sysUserService.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(resResultList)) {
            SysUser sysUser = resResultList.get(0);
            SysUserVo targetUser = new SysUserVo();
            BeanUtils.copyProperties(sysUser, targetUser);
            return targetUser;
        }
        return null;
    }

    @Override
    public void deleteBatchUserIds(@RequestBody List<Long> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return;
        }
        sysUserService.deleteBatchIds(userIds);
    }

    @Override
    public void addUserRoles(@RequestBody SysUserRoleReq userRoleReq) {
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userRoleReq.getUserId());
        sysUserRoleService.delete(queryWrapper);

        userRoleReq.getRoleIds().forEach(roleId -> {
            SysUserRole targetUserRole = new SysUserRole();
            targetUserRole.setUserId(userRoleReq.getUserId());
            targetUserRole.setRoleId(roleId);
            sysUserRoleService.insert(targetUserRole);
        });
    }

    @Override
    public List<SysUserRoleVo> findUserRolesByUserId(@RequestParam("userId") Long userId) {
        List<SysUserRoleVo> targetResultList = Lists.newArrayList();
        LambdaQueryWrapper<SysUserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> userRoleList = sysUserRoleService.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(userRoleList)) {
            for (SysUserRole sysUserRole : userRoleList) {
                SysUserRoleVo userRoleVo = new SysUserRoleVo();
                BeanUtils.copyProperties(sysUserRole, userRoleVo);
                targetResultList.add(userRoleVo);
            }
        }
        return targetResultList;
    }
}
